package cn.funeralobjects.asms.web.controller;

import cn.funeralobjects.ams.web.api.ProductApiApi;
import cn.funeralobjects.ams.web.utils.ApiUtils;
import cn.funeralobjects.asms.entity.ESmsTemplate;
import cn.funeralobjects.asms.model.SmsTemplate;
import cn.funeralobjects.asms.service.SmsTemplateService;
import cn.funeralobjects.asms.web.mapper.SmsTemplateMapper;
import cn.funeralobjects.asms.web.model.MSmsTemplate;
import cn.funeralobjects.asms.web.model.VSmsTemplate;
import cn.funeralobjects.common.service.EntityService;
import cn.funeralobjects.common.service.IdService;
import cn.funeralobjects.common.service.exception.LackDataException;
import cn.funeralobjects.common.web.controller.RestEntityController;
import cn.funeralobjects.common.web.controller.converter.VoMapper;
import cn.funeralobjects.utils.aliyun.sms.SmsAction;
import cn.funeralobjects.utils.aliyun.sms.model.QuerySmsTemplateParam;
import cn.funeralobjects.web.api.SmsTemplateApi;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/6/1 6:22 AM
 */
@RestController
@RequestMapping("/sms-template")
public class SmsTemplateController implements SmsTemplateApi, RestEntityController<VSmsTemplate, ESmsTemplate, Integer> {

    @Resource
    private SmsTemplateService smsTemplateService;

    @Resource
    private SmsTemplateMapper smsTemplateMapper;

    @Resource
    private ProductApiApi productApiApi;

    @PostMapping
    @Override
    public Mono<VSmsTemplate> add(@RequestBody MSmsTemplate mSmsTemplate) {
        return ApiUtils.createClient(mSmsTemplate.getProdResCode(), SmsAction.QUERY_SMS_TEMPLATE, productApiApi)
                .map(client -> ApiUtils.doAction(client, (new QuerySmsTemplateParam(mSmsTemplate.getTemplateCode()))))
                .map(data -> {
                    if (data == null) {
                        throw new LackDataException(String.format("Can't find template[%s]", mSmsTemplate.getTemplateCode()));
                    }
                    return this.toVo(smsTemplateService.add(new SmsTemplate()
                            .setCreateDate(data.getCreateDate())
                            .setProdResCode(mSmsTemplate.getProdResCode())
                            .setReason(data.getReason())
                            .setType(data.getTemplateType())
                            .setTemplateCode(data.getTemplateCode())
                            .setTemplateName(data.getTemplateName())
                            .setTemplateContent(data.getTemplateContent())
                            .setStatus(data.getTemplateStatus())));
                });
    }

    @Override
    public EntityService<ESmsTemplate, Integer> entityService() {
        return smsTemplateService;
    }


    @Override
    public IdService<Integer> idService() {
        return smsTemplateService;
    }

    @Override
    public VoMapper<ESmsTemplate, VSmsTemplate> voMapper() {
        return smsTemplateMapper;
    }
}
